using Sirenix.OdinInspector;
using UnityEngine;

namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 构建信息
    /// </summary>
    [CreateAssetMenu(menuName = "AutoBuild/IQIGame BuildInfo")]
    public class BuildInfo : ScriptableObject
    {
        [ReadOnly]
        [SerializeField]
        private string m_FileName = "BuildInfo";
        /// <summary>
		/// SDK名称
		/// </summary>
        [ReadOnly]
        [SerializeField]
        private string m_SDKName = "Builtin";
        /// <summary>
        /// 平台渠道名称
        /// </summary>
        [ReadOnly]
        [SerializeField]
        private string m_ChannelName = "none";
        /// <summary>
        /// 登录服务器地址
        /// </summary>
        [SerializeField]
        private string m_LoginUrl;
        /// <summary>
        /// 资源服务器地址
        /// </summary>
        [SerializeField]
        private string m_ResourceUrl;
        /// <summary>
        /// Version信息文件地址
        /// </summary>
        [SerializeField]
        private string m_CheckVersionPath;
        /// <summary>
        /// 媒体文件夹地址
        /// </summary>
        [SerializeField]
        private string m_MediaPath;
        /// <summary>
        /// 资源包路径
        /// </summary>
        [SerializeField]
        private string m_GameUpdatePath;
        /// <summary>
        /// 更新包地址
        /// </summary>
        [SerializeField]
        private string m_UpdateAppPath;

        public string FileName => m_FileName;
        public string SDKName => m_SDKName;
        public string ChannelName => m_ChannelName;
        public string LoginUrl => m_LoginUrl;

        //这里必须判断string.IsNullOrEmpty, 因为在编辑器模式下第一次计算的时候可能得到"", 这是错误的结果, 因为BuildInfo此时没有被完全反序列化
        public string CheckVersionUrl => !string.IsNullOrEmpty(m_FullCheckVersionUrl) ? m_FullCheckVersionUrl : (m_FullCheckVersionUrl = CombineResUrl(m_ResourceUrl, m_CheckVersionPath));
        public string MediaUrl => !string.IsNullOrEmpty(m_FullMediaUrl) ? m_FullMediaUrl : (m_FullMediaUrl = CombineResUrl(m_ResourceUrl, m_MediaPath));
        public string GameUpdateUrl => !string.IsNullOrEmpty(m_FullGameUpdateUrl) ? m_FullGameUpdateUrl : (m_FullGameUpdateUrl = CombineResUrl(m_ResourceUrl, m_GameUpdatePath));
        public string UpdateAppUrl => !string.IsNullOrEmpty(m_FullUpdateAppUrl) ? m_FullUpdateAppUrl : (m_FullUpdateAppUrl = CombineResUrl(m_ResourceUrl, m_UpdateAppPath));

        #region Convenience optimization
        private string m_FullCheckVersionUrl;
        private string m_FullMediaUrl;
        private string m_FullGameUpdateUrl;
        private string m_FullUpdateAppUrl;

        private string CombineResUrl(string prefix, string ext)
        {
            //以http开头
            if (ext.StartsWith("http"))
            {
                return ext;
            }
            else
            {
                if (ext.Length == 0)
                {
                    return prefix;
                }
                return $"{prefix}/{ext}";
            }
        }
        #endregion
    }
}
